package com.ym.core.utils;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 *
 */
public class DateKit extends org.apache.commons.lang3.time.DateUtils {
	public static final String YYYY = "yyyy";

	public static final String YYYY_MM = "yyyy-MM";

	public static final String YYYY_MM_DD = "yyyy-MM-dd";

	public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

	public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

	public static final String YYYYMMDD = "yyyy年MM月dd日";

	private static final String[] parsePatterns = {
			"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
			"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
			"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM",
			"yyyyMMdd"};

	/**
	 * 获取当前日期, 默认格式为yyyy-MM-dd
	 *
	 * @return String
	 */
	public static String getDate() {
		return dateTimeNow(YYYY_MM_DD);
	}

	public static final String getTime() {
		return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
	}

	public static final String dateTimeNow() {
		return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
	}

	public static final String dateTimeNowToString() {
		return dateTimeNow(YYYYMMDD);
	}

	public static final String dateTimeNow(final String format) {
		return parseDateToStr(format, new Date());
	}

	public static final String dateTime(final Date date) {
		return parseDateToStr(YYYY_MM_DD, date);
	}

	public static final String parseDateToStr(final String format, final Date date) {
		return new SimpleDateFormat(format).format(date);
	}

	/**
	 * 将某个时间转为指定的格式
	 * @param format
	 * @param date
	 * @return
	 */
	public static final String parseStrToStr(final String format, final String date) {
		Date dateParse = parseDate(date);
		return parseDateToStr(format,dateParse);
	}

	/**
	 * 获取当前时间的前一天
	 */
	public static final String getCurrentBeforeDay(final String format){
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -1); //得到前一天
		Date date = calendar.getTime();
		return new SimpleDateFormat(format).format(date);
	}

	/**
	 * 日期路径 即年/月/日 如2018/08/08
	 */
	public static final String datePath() {
		Date now = new Date();
		return DateFormatUtils.format(now, "yyyy/MM/dd");
	}

	/**
	 * 日期路径 即年/月/日 如20180808
	 */
	public static final String dateTime() {
		Date now = new Date();
		return DateFormatUtils.format(now, "yyyyMMdd");
	}


	/**
	 * 日期型字符串转化为日期 格式
	 */
	public static Date parseDate(String str) {
		if (str == null) {
			return null;
		}
		try {
			return parseDate(str.toString(), parsePatterns);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 获取服务器启动时间
	 */
	public static Date getServerStartDate() {
		long time = ManagementFactory.getRuntimeMXBean().getStartTime();
		return new Date(time);
	}

	/**
	 * 计算两个时间差
	 */
	public static String getDatePoor(Date endDate, Date nowDate) {
		long nd = 1000 * 24 * 60 * 60L;
		long nh = 1000 * 60 * 60L;
		long nm = 1000 * 60L;
		// long ns = 1000;
		// 获得两个时间的毫秒时间差异
		long diff = endDate.getTime() - nowDate.getTime();
		// 计算差多少天
		long day = diff / nd;
		// 计算差多少小时
		long hour = diff % nd / nh;
		// 计算差多少分钟
		long min = diff % nd % nh / nm;
		// 计算差多少秒//输出结果
		// long sec = diff % nd % nh % nm / ns;
		return day + "天" + hour + "小时" + min + "分钟";
	}

	/**
	 * 获取未来某天的凌晨时间
	 */
	public static Date getFutureFirstTime(int num) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, num);
		Date date = calendar.getTime();
		String str = parseDateToStr(YYYY_MM_DD, date) + " 00:00:00";
		return parseDate(str);
	}

	/**
	 * 获取未来某天前一天的末尾时间
	 */
	public static Date getFutureLastTime(int num) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, num - 1);
		Date date = calendar.getTime();
		String str = parseDateToStr(YYYY_MM_DD, date) + " 23:59:59";
		return parseDate(str);
	}

	/**
	 * 获取某月的第一天
	 *
	 * @param month 为NULL标识当用
	 * @return
	 */
	public static Date getFirstDayOfMonth(Integer year, Integer month) {
		Calendar instance = Calendar.getInstance();
		if (month != null) {
			instance.set(Calendar.MONTH, month - 1);
		}
		if (year != null) {
			instance.set(Calendar.YEAR, year);
		}
		int minimum = instance.getActualMinimum(Calendar.DAY_OF_MONTH);
		instance.set(Calendar.DAY_OF_MONTH, minimum);
		Date date = instance.getTime();
		String str = parseDateToStr(YYYY_MM_DD, date) + " 00:00:00";
		return parseDate(str);
	}

	/**
	 * 获取某月的最后一天
	 *
	 * @param month 为NULL标识当用
	 * @return
	 */
	public static Date getLastDayOfMonth(Integer year, Integer month) {
		Calendar instance = Calendar.getInstance();
		if (month != null) {
			instance.set(Calendar.MONTH, month - 1);
		}
		if (year != null) {
			instance.set(Calendar.YEAR, year);
		}
		int maximum = instance.getActualMaximum(Calendar.DAY_OF_MONTH);
		instance.set(Calendar.DAY_OF_MONTH, maximum);
		Date date = instance.getTime();
		String str = parseDateToStr(YYYY_MM_DD, date) + " 23:59:59";
		return parseDate(str);
	}

	/**
	 * 获取某年的第一天
	 *
	 * @param year 为NULL表示当前年份
	 * @return
	 */
	public static Date getFirstDayOfYear(Integer year) {
		Calendar instance = Calendar.getInstance();
		if (year != null) {
			instance.set(Calendar.YEAR, year);
		}
		instance.set(Calendar.MONTH, Calendar.JANUARY);
		int day = instance.getActualMinimum(Calendar.DAY_OF_MONTH);
		instance.set(Calendar.DAY_OF_MONTH, day);
		Date date = instance.getTime();
		String str = parseDateToStr(YYYY_MM_DD, date) + " 00:00:00";
		return parseDate(str);
	}

	/**
	 * 获取某年的最后一天
	 *
	 * @param year 为NULL表示当前年
	 * @return
	 */
	public static Date getLastDayOfYear(Integer year) {
		Calendar instance = Calendar.getInstance();
		if (year != null) {
			instance.set(Calendar.YEAR, year);
		}
		instance.set(Calendar.MONTH, Calendar.DECEMBER);
		int day = instance.getActualMaximum(Calendar.DAY_OF_MONTH);
		instance.set(Calendar.DAY_OF_MONTH, day);
		Date date = instance.getTime();
		String str = parseDateToStr(YYYY_MM_DD, date) + " 23:59:59";
		return parseDate(str);
	}

	/**
	 * 获取某一年某一季度的第一天
	 *
	 * @param year
	 * @param season
	 * @return
	 */
	static Date getSeasonFirstDay(int year, int season) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
		cal.set(Calendar.YEAR, year);
		if (season == 1) {
			cal.set(Calendar.MONTH, 0);
		} else if (season == 2) {
			cal.set(Calendar.MONTH, 3);
		} else if (season == 3) {
			cal.set(Calendar.MONTH, 6);
		} else {
			cal.set(Calendar.MONTH, 9);
		}
		return cal.getTime();
	}

	/**
	 * 获取某一年某一季度的最后一天
	 *
	 * @param year
	 * @param season
	 * @return
	 */
	static Date getSeasonLastDay(int year, int season) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		cal.set(Calendar.YEAR, year);
		if (season == 1) {
			cal.set(Calendar.MONTH, 2);
		} else if (season == 2) {
			cal.set(Calendar.MONTH, 5);
		} else if (season == 3) {
			cal.set(Calendar.MONTH, 8);
		} else {
			cal.set(Calendar.MONTH, 11);
		}
		return cal.getTime();
	}

	/**
	 * 根据日期获取当前的季度
	 *
	 * @param date
	 * @return
	 */
	public static int getSeason(Date date) {
		int season = 0;
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int month = c.get(Calendar.MONTH);
		switch (month) {
			case Calendar.JANUARY:
			case Calendar.FEBRUARY:
			case Calendar.MARCH:
				season = 1;
				break;
			case Calendar.APRIL:
			case Calendar.MAY:
			case Calendar.JUNE:
				season = 2;
				break;
			case Calendar.JULY:
			case Calendar.AUGUST:
			case Calendar.SEPTEMBER:
				season = 3;
				break;
			case Calendar.OCTOBER:
			case Calendar.NOVEMBER:
			case Calendar.DECEMBER:
				season = 4;
				break;
			default:
				break;
		}
		return season;
	}

	/**获取某个时间前几天/后几天 的时间
	 * @param date
	 * @param years
	 * @param months
	 * @param dates
	 * @param hours
	 * @param minutes
	 * @param seconds
	 * @param format
	 * @return
	 */
	public static final String getBeforeTime(Date date, int years, int months, int dates, int hours, int minutes, int seconds,String format) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, years);
		calendar.add(Calendar.MONTH, months);
		calendar.add(Calendar.DATE, dates);
		calendar.add(Calendar.HOUR, hours);
		calendar.add(Calendar.MINUTE, minutes);
		calendar.add(Calendar.SECOND, seconds);
		Date time = calendar.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String beforeTime = sdf.format(time);
		return beforeTime;
	}


	// 获得上个月月份年份
	public static String getYearMonth() {
		Calendar now = Calendar.getInstance();
		int month = now.get(Calendar.MONTH);
		String m;
		if (month < 10) {
			m = "0" + month;
		} else {
			m = month + "";
		}
		return now.get(Calendar.YEAR) + "-" + m;
	}


	public static void main(String[] args) {
	/*	System.out.println(getFirstMonthTime(new Date()));
		System.out.println("-------------------------------------------------");

		String beforeTime = DateKit.getBeforeTime(new Date(), 0, 0, 0, 0, -5, 0, DateKit.YYYY_MM_DD_HH_MM_SS);

		System.out.println(beforeTime);*/
		//System.out.println(getYearMonth());
		//System.out.println(parseDateToStr("yyyyMMdd",parseDate("20191217")));
		System.out.println(DateKit.getBeforeTime(new Date(),0,0,1,0,0,0, DateKit.YYYY_MM_DD));
	}



}
